﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NEWS;
using System.Data;

namespace NEWS.DAL
{
    public class Menu
    {
        public static bool update(NEWS.Model.Menu menu)
        {
            bool result = false;

            string strsql = "update t_menu set title='"+menu.Title+"',state="+menu.State+" where id = "+menu.Id+"";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;

        }

        public static bool del(int id)
        {
            bool result = false;

            string strsql = "delete from t_menu where id = " + id + "";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;

        }

        public static NEWS.Model.Menu GetMenu(int id)
        {
            string strsql = "select * from t_menu where id = '"+id+"'";
            DataTable dataTable = NEWS.MsSqlHelper.YFMsSqlHelper.Query(strsql).Tables[0];
            return Dttolist(dataTable)[0];
        }

        public static bool add(NEWS.Model.Menu menu)
        {
            bool result = false;

            string strsql = "insert into t_menu (title,state) values ('"+menu.Title+"',"+menu.State+")";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;
        }

        public static List<NEWS.Model.Menu> list()
        { 
            string strsql = "select * from t_menu";
            DataTable dataTable = NEWS.MsSqlHelper.YFMsSqlHelper.Query(strsql).Tables[0];
            return Dttolist(dataTable);
        }

        public static List<NEWS.Model.Menu> Dttolist(DataTable dt)
        {
            List<NEWS.Model.Menu> list = new List<Model.Menu>();
            
            for(int i = 0;i < dt.Rows.Count;i++)
            {
                NEWS.Model.Menu menu = new Model.Menu();

                menu.Id = int.Parse(dt.Rows[i]["id"].ToString());
                menu.State = int.Parse(dt.Rows[i]["state"].ToString());
                menu.Title = dt.Rows[i]["title"].ToString();

                list.Add(menu);
            }

            return list;
        }
    }
}
